package test2.spider.news.tencent;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;

public class Tencent {
	
	static String[] channels = new String[]{"hgjj", "jrsc", "gjcj", "gsbd", "xfwt"};
	static String link = "http://finance.qq.com/";
	//http://finance.qq.com/c/hgjjllist_3.htm
	static String link_prefix = "http://finance.qq.com/c/";
	static String link_middle = "llist_";
	static String link_suffix = ".htm";
	
	static String url = "jdbc:mysql://localhost/demo?characterEncoding=UTF-8";
	static String username = "root";
	static String password = "";
	public static ExecutorService threadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

	static {
		DruidPlugin dp = new DruidPlugin(url, username, password);
		ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
		arp.setDevMode(true);
		arp.setShowSql(true);
		dp.start();
		arp.start();
	}
	
	public static void main(String[] args) {
		long l1 = System.currentTimeMillis();
		
		for(String channel : channels) {
			link = link + channel + link_suffix;
			new MainFirstProcessor().process(link, "gbk");
			int page = 2;
			while(true) {
				String otherlink = link_prefix + channel + link_middle + (page++) + link_suffix;
				try {
					new MainOtherProcessor().process(otherlink, "gbk");
				} catch (Exception e) {
					System.out.println(e.getMessage());
					if(e.getMessage().equals("the end")) {
						break;
					}
				}
			}
		}
		
		threadPool.shutdown();
		Runtime.getRuntime().addShutdownHook(new Thread() {
			@Override
			public void run() {
				long l2 = System.currentTimeMillis();
				System.out.println("共耗时：" + (l2-l1)/1000 + "s");
			}
		});
	}

}
